<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.9.1"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Eigen: Inplace matrix decompositions</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  $(document).ready(function() { init_search(); });
/* @license-end */
</script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
</script>
<script type="text/javascript" async="async" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="eigendoxy.css" rel="stylesheet" type="text/css">
<!--  -->
<script type="text/javascript" src="eigen_navtree_hacks.js"></script>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="Eigen_Silly_Professor_64x64.png"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname"><a href="http://eigen.tuxfamily.org">Eigen</a>
   &#160;<span id="projectnumber">3.4.90 (git rev 67eeba6e720c5745abc77ae6c92ce0a44aa7b7ae)</span>
   </div>
  </td>
   <td>        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.svg"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.svg" alt=""/></a>
          </span>
        </div>
</td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.1 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search','.html');
/* @license-end */
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('group__InplaceDecomposition.html',''); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">Inplace matrix decompositions<div class="ingroups"><a class="el" href="group__DenseLinearSolvers__chapter.html">Dense linear problems and decompositions</a></div></div>  </div>
</div><!--header-->
<div class="contents">
<p>Starting from Eigen 3.3, the LU, Cholesky, and QR decompositions can operate <em>inplace</em>, that is, directly within the given input matrix. This feature is especially useful when dealing with huge matrices, and or when the available memory is very limited (embedded systems).</p>
<p>To this end, the respective decomposition class must be instantiated with a Ref&lt;&gt; matrix type, and the decomposition object must be constructed with the input matrix as argument. As an example, let us consider an inplace LU decomposition with partial pivoting.</p>
<p>Let's start with the basic inclusions, and declaration of a 2x2 matrix <code>A:</code> </p>
<table class="example">
<tr>
<th>code</th><th>output </th></tr>
<tr>
<td><div class="fragment"><div class="line">};</div>
<div class="line">init init_obj;</div>
</div><!-- fragment -->  </td><td><div class="fragment"><div class="line">Here is the input matrix A before decomposition:</div>
<div class="line"> 2 -1</div>
<div class="line"> 1  3</div>
</div><!-- fragment -->   </td></tr>
</table>
<p>No surprise here! Then, let's declare our inplace LU object <code>lu</code>, and check the content of the matrix <code>A:</code> </p>
<table class="example">
<tr>
<td><div class="fragment"><div class="line">  <a class="code" href="classEigen_1_1PartialPivLU.html">Eigen::PartialPivLU&lt;Eigen::Ref&lt;Eigen::MatrixXd&gt;</a> &gt; lu(A);</div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;Here is the input matrix A after decomposition:\n&quot;</span> &lt;&lt; A &lt;&lt; <span class="stringliteral">&quot;\n&quot;</span>;</div>
<div class="ttc" id="aclassEigen_1_1PartialPivLU_html"><div class="ttname"><a href="classEigen_1_1PartialPivLU.html">Eigen::PartialPivLU</a></div><div class="ttdoc">LU decomposition of a matrix with partial pivoting, and related features.</div><div class="ttdef"><b>Definition:</b> PartialPivLU.h:80</div></div>
</div><!-- fragment -->  </td><td><div class="fragment"><div class="line">Here is the input matrix A after decomposition:</div>
<div class="line">  2  -1</div>
<div class="line">0.5 3.5</div>
</div><!-- fragment -->   </td></tr>
</table>
<p>Here, the <code>lu</code> object computes and stores the <code>L</code> and <code>U</code> factors within the memory held by the matrix <code>A</code>. The coefficients of <code>A</code> have thus been destroyed during the factorization, and replaced by the L and U factors as one can verify:</p>
<table class="example">
<tr>
<td><div class="fragment"><div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;Here is the matrix storing the L and U factors:\n&quot;</span> &lt;&lt; lu.matrixLU() &lt;&lt; <span class="stringliteral">&quot;\n&quot;</span>;</div>
</div><!-- fragment -->  </td><td><div class="fragment"><div class="line">Here is the matrix storing the L and U factors:</div>
<div class="line">  2  -1</div>
<div class="line">0.5 3.5</div>
</div><!-- fragment -->   </td></tr>
</table>
<p>Then, one can use the <code>lu</code> object as usual, for instance to solve the Ax=b problem: </p><table class="example">
<tr>
<td><div class="fragment"><div class="line">  <a class="code" href="classEigen_1_1Matrix.html">Eigen::MatrixXd</a> A0(2,2); A0 &lt;&lt; 2, -1, 1, 3;</div>
<div class="line">  <a class="code" href="classEigen_1_1Matrix.html">Eigen::VectorXd</a> b(2);    b &lt;&lt; 1, 2;</div>
<div class="line">  <a class="code" href="classEigen_1_1Matrix.html">Eigen::VectorXd</a> x = lu.solve(b);</div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;Residual: &quot;</span> &lt;&lt; (A0 * x - b).norm() &lt;&lt; <span class="stringliteral">&quot;\n&quot;</span>;</div>
<div class="ttc" id="aclassEigen_1_1Matrix_html"><div class="ttname"><a href="classEigen_1_1Matrix.html">Eigen::Matrix</a></div><div class="ttdoc">The matrix class, also used for vectors and row-vectors.</div><div class="ttdef"><b>Definition:</b> Matrix.h:182</div></div>
</div><!-- fragment -->  </td><td><div class="fragment"><div class="line">Residual: 0</div>
</div><!-- fragment -->   </td></tr>
</table>
<p>Here, since the content of the original matrix <code>A</code> has been lost, we had to declared a new matrix <code>A0</code> to verify the result.</p>
<p>Since the memory is shared between <code>A</code> and <code>lu</code>, modifying the matrix <code>A</code> will make <code>lu</code> invalid. This can easily be verified by modifying the content of <code>A</code> and trying to solve the initial problem again:</p>
<table class="example">
<tr>
<td><div class="fragment"><div class="line">  A &lt;&lt; 3, 4, -2, 1;</div>
<div class="line">  x = lu.solve(b);</div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;Residual: &quot;</span> &lt;&lt; (A0 * x - b).norm() &lt;&lt; <span class="stringliteral">&quot;\n&quot;</span>;</div>
</div><!-- fragment -->  </td><td><div class="fragment"><div class="line">Residual: 15.8114</div>
</div><!-- fragment -->   </td></tr>
</table>
<p>Note that there is no shared pointer under the hood, it is the <b>responsibility</b> <b>of</b> <b>the</b> <b>user</b> to keep the input matrix <code>A</code> in life as long as <code>lu</code> is living.</p>
<p>If one wants to update the factorization with the modified A, one has to call the compute method as usual: </p><table class="example">
<tr>
<td><div class="fragment"><div class="line">  A0 = A; <span class="comment">// save A</span></div>
<div class="line">  lu.compute(A);</div>
<div class="line">  x = lu.solve(b);</div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;Residual: &quot;</span> &lt;&lt; (A0 * x - b).norm() &lt;&lt; <span class="stringliteral">&quot;\n&quot;</span>;</div>
</div><!-- fragment -->  </td><td><div class="fragment"><div class="line">Residual: 0</div>
</div><!-- fragment -->   </td></tr>
</table>
<p>Note that calling compute does not change the memory which is referenced by the <code>lu</code> object. Therefore, if the compute method is called with another matrix <code>A1</code> different than <code>A</code>, then the content of <code>A1</code> won't be modified. This is still the content of <code>A</code> that will be used to store the L and U factors of the matrix <code>A1</code>. This can easily be verified as follows: </p><table class="example">
<tr>
<td><div class="fragment"><div class="line">  <a class="code" href="classEigen_1_1Matrix.html">Eigen::MatrixXd</a> A1(2,2);</div>
<div class="line">  A1 &lt;&lt; 5,-2,3,4;</div>
<div class="line">  lu.compute(A1);</div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;Here is the input matrix A1 after decomposition:\n&quot;</span> &lt;&lt; A1 &lt;&lt; <span class="stringliteral">&quot;\n&quot;</span>;</div>
</div><!-- fragment -->  </td><td><div class="fragment"><div class="line">Here is the input matrix A1 after decomposition:</div>
<div class="line"> 5 -2</div>
<div class="line"> 3  4</div>
</div><!-- fragment -->   </td></tr>
</table>
<p>The matrix <code>A1</code> is unchanged, and one can thus solve A1*x=b, and directly check the residual without any copy of <code>A1:</code> </p><table class="example">
<tr>
<td><div class="fragment"><div class="line">  x = lu.solve(b);</div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;Residual: &quot;</span> &lt;&lt; (A1 * x - b).norm() &lt;&lt; <span class="stringliteral">&quot;\n&quot;</span>;</div>
</div><!-- fragment -->  </td><td><div class="fragment"><div class="line">Residual: 2.48253e-16</div>
</div><!-- fragment -->   </td></tr>
</table>
<p>Here is the list of matrix decompositions supporting this inplace mechanism:</p>
<ul>
<li>class <a class="el" href="classEigen_1_1LLT.html" title="Standard Cholesky decomposition (LL^T) of a matrix and associated features.">LLT</a></li>
<li>class <a class="el" href="classEigen_1_1LDLT.html" title="Robust Cholesky decomposition of a matrix with pivoting.">LDLT</a></li>
<li>class <a class="el" href="classEigen_1_1PartialPivLU.html" title="LU decomposition of a matrix with partial pivoting, and related features.">PartialPivLU</a></li>
<li>class <a class="el" href="classEigen_1_1FullPivLU.html" title="LU decomposition of a matrix with complete pivoting, and related features.">FullPivLU</a></li>
<li>class <a class="el" href="classEigen_1_1HouseholderQR.html" title="Householder QR decomposition of a matrix.">HouseholderQR</a></li>
<li>class <a class="el" href="classEigen_1_1ColPivHouseholderQR.html" title="Householder rank-revealing QR decomposition of a matrix with column-pivoting.">ColPivHouseholderQR</a></li>
<li>class <a class="el" href="classEigen_1_1FullPivHouseholderQR.html" title="Householder rank-revealing QR decomposition of a matrix with full pivoting.">FullPivHouseholderQR</a></li>
<li>class <a class="el" href="classEigen_1_1CompleteOrthogonalDecomposition.html" title="Complete orthogonal decomposition (COD) of a matrix.">CompleteOrthogonalDecomposition</a> </li>
</ul>
</div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated on Thu Apr 21 2022 13:07:55 for Eigen by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.9.1 </li>
  </ul>
</div>
</body>
</html>
